import modelEntity from "./modelEntity";
import PersonnelInfo from "./PersonnelInfo"
import UserRole from "./UserRole"
export default class UserInfo extends modelEntity {
  constructor(options) {
    super(options);
    this.LoginName = options.LoginName;
    this.LoginPassword = options.LoginPassword;
    this.PerInfoID = options.PerInfoID;
    this.State = options.State;
    this.WeChatPhone = options.WeChatPhone;
    this.OpenID = options.OpenID;
    this.personnelInfoName = "";
    if (options.personnelInfo) {
      this.personnelInfo = new PersonnelInfo(options.personnelInfo);
      this.personnelInfoName = this.personnelInfo.Name;
    }

    this.userRoles = [];
    if (options.userRoles) {
      options.userRoles.forEach(x => {
        this.userRoles.push(new UserRole(x));
      })
    }
  }
  get roleStr() {
    let list = [];
    this.userRoles.forEach(x => {
      if (x.roleTemplate) {
        if (x.Param) {
          let find = window.sys.project_list.find(y => y.GuidID == x.Param);
          if (find) {
            list.push(`${x.roleTemplate.Name}(${find.PName})`)
          }

        } else {
          list.push(x.roleTemplate.Name)
        }

      }

    });
    return list.join(',');
  }
  static newInstance() {
    return new UserInfo({
      LoginName: null,
      LoginPassword: null,
      PerInfoID: null,
      State: null,
      WeChatPhone: null,
      OpenID: null,
    })
  }
  get Name() {
    if (this.personnelInfo) {
      return this.personnelInfo.Name;
    } else {
      return "";
    }
  }

  static load_my() {
    return new Promise((resolve, reject) => {
      getApp().globalData.api.http_get(`${getApp().globalData.baseUrl}/api/sys/user/my`)
        .then(
          res => {
            let user = new UserInfo(res);
            window.sessionStorage.setItem('user', JSON.stringify(user));
            resolve(user);
          }
        )
    })
  }
  static loadList() {
    return new Promise((resolve, reject) => {
      getApp().globalData.api.http_get(`${getApp().globalData.baseUrl}/api/sys/users`)
        .then(
          res => {
            console.log(res);
            let list = [];
            res.forEach(x => {
              list.push(new UserInfo(x));
            });
            resolve(list);
          }
        )
    })
  }
  create() {
    return new Promise((resolve, reject) => {
      window.api.http_post("/api/sys/user", this)
        .then(
          res => {
            let model = new UserInfo(res);
            resolve(model);
          }
        )
    })
  }
  createwx() {
    return new Promise((resolve, reject) => {
      window.api.http_post("/api/sys/user/wx", this)
        .then(
          res => {
            let model = new UserInfo(res);
            resolve(model);
          }
        )
    })
  }
  delete() {
    return new Promise((resolve, reject) => {
      window.api.http_delete("/api/sys/user", this)
        .then(
          res => {
            resolve(res);
          }
        )
    })
  }
  modify() {
    return new Promise((resolve, reject) => {
      window.api.http_put("/api/sys/user", this)
        .then(
          res => {
            let model = new UserInfo(res);
            resolve(model);
          }
        )
    })
  }
}
